package com.aya.simple;

import com.aya.simple.service.ISessionService;
import com.aya.simple.service.SessionService;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * HttpServletResponse 两种方式注入的线程安全演示
 */
@Controller
public class ResponseController {

    @Resource
    private ApplicationContext applicationContext;
    @Resource
    ISessionService sessionService;

    @RequestMapping("session")
    public String session() throws IOException {
//        final SessionService bean = applicationContext.getBean(SessionService.class);
//        bean.sayHello();

        sessionService.sayHello();

        return "hello";
    }


    @RequestMapping("hello/mapping")
    public String hello(String name, String age, String text) throws IOException {
        System.out.println("name:" + name);
        System.out.println("age:" + age);
        System.out.println("text:" + text);
        return "hello";
    }


    @RequestMapping("methodInject")
    public void methodInject(HttpServletResponse myResponse) throws IOException {
        myResponse.getWriter().println("methodInject");
    }




    private HttpServletResponse localResponse;

    @ModelAttribute
    public void local(HttpServletResponse response)  {
        this.localResponse = response;
    }

    @RequestMapping("localA")
    public void localA() throws IOException {
        this.localResponse .getWriter().println("localA");
    }

    @RequestMapping("localB")
    public void localB() throws IOException, InterruptedException {
        Thread.sleep(5000);
        this.localResponse .getWriter().println("localB");
    }


    @Resource
    private HttpServletResponse response;

    @RequestMapping("memberA")
    public void memberA() throws IOException {
        response.getWriter().println("memberA");
    }

    @RequestMapping("memberB")
    public void memberB() throws IOException, InterruptedException {
        Thread.sleep(5000);
        response.getWriter().println("memberB");
    }
}
